﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuDao.Api
{
    /// <summary>
    /// 对接口返回数据进行统一包装
    /// </summary>
    public class ApiResultFilterAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            var code = 200;
            var msg = "OK";
            context.Result = context.Result switch
            {
                ObjectResult result => new JsonResult(new { code, msg, data = result.Value }) { StatusCode = code },
                ContentResult result => new JsonResult(new { code, msg, data = result.Content }) { StatusCode = code, ContentType = result.ContentType },
                _ => new JsonResult(new { code, msg }) { StatusCode = code },
            };
            base.OnResultExecuting(context);
        }
    }
}